package cn.easy.jacob.rocketmq;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RocketMQClient {
    private static final Logger LOGGER = LoggerFactory.getLogger(MQProducer.class);

    @Autowired
    private DefaultMQProducer mqProducer;
    public void sendMessage(Message message) throws InterruptedException, RemotingException, MQClientException, MQBrokerException {
        mqProducer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                LOGGER.info("MQ: 生产者发送消息 {}", sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                LOGGER.error(throwable.getMessage(), throwable);

            }
        },10000);
    }
}
